package com.abe.libquick.widget;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.abe.libquick.R;


public class TNTPopupWindow {
    /**
     * @param cx    activity
     * @param view  传入需要显示在什么控件下
     * @param view1 传入内容的view
     */
    public static PopupWindow makePopupWindow(Context cx, View view, View view1) {
        if (view == null || view1 == null) return new PopupWindow(cx);
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager wmManager = (WindowManager) cx.getSystemService(Context.WINDOW_SERVICE);
        wmManager.getDefaultDisplay().getMetrics(dm);
        int Height = dm.heightPixels;
        PopupWindow window = new PopupWindow(cx);
        window.setBackgroundDrawable(new ColorDrawable(cx.getResources().getColor(R.color.txt_c)));
        view1.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        // 设置PopupWindow的大小（宽度和高度）
        window.setWidth(view.getWidth());
        window.setHeight((Height - view.getBottom()) * 2 / 3);
        window.setContentView(view1);// 设置PopupWindow的内容view
        window.setFocusable(true); // 设置PopupWindow可获得焦点
        window.setTouchable(true); // 设置PopupWindow可触摸
        window.setOutsideTouchable(true); // 设置非PopupWindow区域可触摸
        return window;
    }

    /**
     * 固定出现在屏幕的中间
     *
     * @param cx   cx
     * @param view view
     */
    public static PopupWindow makePopupWindow(Context cx, View view) {
        if (view == null) return new PopupWindow(cx);
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager wmManager = (WindowManager) cx.getSystemService(Context.WINDOW_SERVICE);
        wmManager.getDefaultDisplay().getMetrics(dm);
        int Height = dm.heightPixels;
        int width = dm.widthPixels;
        PopupWindow window = new PopupWindow(cx);
        window.setBackgroundDrawable(new ColorDrawable(cx.getResources().getColor(R.color.txt_c)));
        view.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        // 设置PopupWindow的大小（宽度和高度）
        window.setWidth(width * 7 / 8);
        window.setHeight(Height * 2 / 3);
        window.setContentView(view);// 设置PopupWindow的内容view
        window.setFocusable(true); // 设置PopupWindow可获得焦点
        window.setTouchable(true); // 设置PopupWindow可触摸
        window.setOutsideTouchable(false); // 设置非PopupWindow区域可触摸
        return window;
    }

    /**
     * 指定大小,并在指定view下方显示
     *
     * @param cx     cx
     * @param view   view
     * @param width  width
     * @param height height
     */
    public static PopupWindow makePopupWindow(Context cx, View view, int width, int height, int colorHint) {
        if (view == null) return new PopupWindow(cx);
        WindowManager wmManager = (WindowManager) cx.getSystemService(Context.WINDOW_SERVICE);
        wmManager.getDefaultDisplay().getMetrics(new DisplayMetrics());
        PopupWindow window = new PopupWindow(cx);
        final Resources res = cx.getResources();
        window.setBackgroundDrawable(new ColorDrawable(res.getColor(colorHint)));
        ViewGroup.LayoutParams LayoutParams = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(LayoutParams);
        // 设置PopupWindow的大小（宽度和高度）
        window.setWidth(width);
        window.setHeight((height));
        window.setContentView(view);// 设置PopupWindow的内容view
        window.setFocusable(true); // 设置PopupWindow可获得焦点
        window.setTouchable(true); // 设置PopupWindow可触摸
        window.setOutsideTouchable(true); // 设置非PopupWindow区域可触摸
        return window;
    }
}